package me.zhengjie.modules.lock.service.impl;

import com.zelk.lock.api.clent.feign.LockDeviceInfoApi;
import com.zelk.lock.api.dto.LockDeviceDetailDTO;
import com.zelk.lock.api.dto.LockDeviceListDTO;
import com.zelk.lock.api.dto.LockDeviceUnbindDTO;
import com.zelk.lock.api.vo.LockDeviceDetailReqVO;
import com.zelk.lock.api.vo.LockDeviceListReqVO;
import com.zelk.lock.api.vo.LockDeviceUnbindReqVO;
import com.zelk.smartlock.common.response.ResponsePage;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.lock.mapping.MappingUtil;
import me.zhengjie.modules.lock.service.LockDeviceService;
import me.zhengjie.modules.lock.service.dto.*;
import me.zhengjie.modules.lock.service.vo.LockDeviceInfoDetailReqVO;
import me.zhengjie.modules.lock.service.vo.LockDeviceInfoListReqVO;
import me.zhengjie.modules.lock.service.vo.LockDeviceInfoUnbindReqVO;
import me.zhengjie.modules.lock.service.vo.LockLeaseBackCloseReqVO;
import me.zhengjie.modules.lock.util.RequestCall;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LockDeviceServiceImpl implements LockDeviceService {

    @Autowired
    private MappingUtil mappingUtil;
    @Autowired
    private LockDeviceInfoApi lockDeviceInfoApi;

    @Override
    public Map<String, Object> selectLockInfoList(LockDeviceInfoListReqVO reqVO, Pageable pageable) {
        LockDeviceListReqVO listReqVO = mappingUtil.toLockDeviceListReqVO(reqVO);
        listReqVO.setPageIdx((long) pageable.getPageNumber());
        listReqVO.setPageSize((long) pageable.getPageSize());
        ResponsePage<List<LockDeviceListDTO>> deviceListPage = RequestCall.executePage(lockDeviceInfoApi::selectLockList, listReqVO, null);
        List<LockDeviceListDTO> deviceList = Optional.ofNullable(deviceListPage.getData()).orElse(new ArrayList<>());
        List<LockDeviceInfoListDTO> deviceInfoListDTOS = deviceList.stream().map(v -> {
            LockDeviceInfoListDTO deviceInfoDTO = mappingUtil.toLockDeviceInfoListDTO(v);
            //TODO 还要填充商户名称
            return deviceInfoDTO;
        }).collect(Collectors.toList());
        return PageUtil.toPage(deviceInfoListDTOS, Objects.nonNull(deviceListPage.getTotal()) ? deviceListPage.getTotal() : 0);
    }

    @Override
    public LockDeviceInfoDetailDTO detail(LockDeviceInfoDetailReqVO reqVO) {
        LockDeviceDetailReqVO detailReqVO = mappingUtil.toLockDeviceDetailReqVO(reqVO);
        LockDeviceDetailDTO detailDTO = RequestCall.execute(lockDeviceInfoApi::lockDetail, detailReqVO, null);
        LockDeviceInfoDetailDTO deviceInfoDetailDTO = mappingUtil.toLockDeviceInfoDetailDTO(detailDTO);

        //TODO 售后查询，查询最新一条
        LockLeaseBackInfoDTO lockLeaseBackInfoDTO = new LockLeaseBackInfoDTO();

        return deviceInfoDetailDTO;
    }

    @Override
    public LockDeviceInfoUnbindDTO unbind(LockDeviceInfoUnbindReqVO reqVO) {
        LockDeviceUnbindReqVO unbindReqVO = mappingUtil.toLockDeviceUnbindReqVO(reqVO);
        LockDeviceUnbindDTO unbindDTO = RequestCall.execute(lockDeviceInfoApi::lockUnbind, unbindReqVO, null);
        LockDeviceInfoUnbindDTO infoUnbindDTO = mappingUtil.toLockDeviceInfoUnbindDTO(unbindDTO);
        return infoUnbindDTO;
    }

    @Override
    public LockLeaseBackCloseDTO closeLockLeaseBack(LockLeaseBackCloseReqVO reqVO) {

        return null;
    }

}
